/**
 * Created by rwecho001 on 14-8-26.
 */



Plane = function() {
    Sim.Object.call(this);
};

Plane.prototype = new Sim.Object();

Plane.prototype.init = function (minV, maxV, step, points, color, faceX,faceY,faceZ) {
    var minX = Math.floor(minV.x / step) * step, minY = Math.floor(minV.y / step) * step, minZ = Math.floor(minV.z / step) * step;
    minV = new THREE.Vector3(minX, minY, minZ);
    var group = new THREE.Object3D();
    this.setObject3D(group);

    var vertices = [];
    var faces = [];
    for (var i = 0; i < points.length; i++) {
        var point = points[i].Vector3;
        vertices.push(new THREE.Vector3(point.x, minV.y, minV.z));
        vertices.push(new THREE.Vector3(point.x, minV.y, maxV.z));
        vertices.push(new THREE.Vector3(point.x, point.y, minV.z));
        vertices.push(new THREE.Vector3(point.x, point.y, maxV.z));
//        faces.push( new THREE.Face3(4*i,4*i+1,4*i+2), new THREE.Face3(4*i+1,4*i+2,4*i+3));


        if (i != points.length - 1 && faceX) {
            faces.push(new THREE.Face3(4 * i, 4 * i + 1, 4 * (i + 1)), new THREE.Face3(4 * i + 1, 4 * (i + 1), 4 * (i + 1) + 1));
//            faces.push( new THREE.Face3(4*i,4*i+2,4*(i+1)), new THREE.Face3(4*i+2,4*(i+1),4*(i+1)+2));
//            faces.push( new THREE.Face3(4*i+2,4*i+3,4*(i+1)+2), new THREE.Face3(4*i+3,4*(i+1)+2,4*(i+1)+3));
//            faces.push( new THREE.Face3(4*i+1,4*i+3,4*(i+1)+3), new THREE.Face3(4*i+1,4*(i+1)+1,4*(i+1)+3));
        }   
    }


    var geom = new THREE.Geometry();
    geom.vertices = vertices;
    geom.faces = faces;
    geom.computeCentroids();
    geom.mergeVertices();
    
 

    //0xF9FF99
    var meshMaterial = new THREE.MeshLambertMaterial({ ambient: color });
    meshMaterial.side = THREE.DoubleSide;
    meshMaterial.transparent = true;
    meshMaterial.opacity = 0.5;

    var mesh = THREE.SceneUtils.createMultiMaterialObject(
        geom, [meshMaterial]);

    group.add(mesh);

    var geometry = new THREE.Geometry();

    for (var j = 0; j < vertices.length; j += 4) {

        var point0 = vertices[j];
        var point1 = vertices[j + 1];
        var point2 = vertices[j + 2];
        var point3 = vertices[j + 3];
        
        if (faceX) {
            geometry.vertices.push(point2, point3);
        }
       

        if (faceY) {
            geometry.vertices.push(point1, point3);

            geometry.vertices.push(point0, point2);
        }
       
        
        if (j != vertices.length - 4) {
            var point6 = vertices[j + 6];
            var point4 = vertices[j + 4];
            for (var z = minV.z; z <= maxV.z; z++) {
                if (faceX) {
                    geometry.vertices.push(new THREE.Vector3(point2.x, point2.y, z));
                    geometry.vertices.push(new THREE.Vector3(point6.x, point6.y, z));
                }
              
                
                if (j == 0 && faceZ) {
                    geometry.vertices.push(new THREE.Vector3(point2.x, point2.y, z));
                    geometry.vertices.push(new THREE.Vector3(point0.x, point0.y, z));
                }
                if (j == vertices.length - 2 * 4 && faceZ) {
                    geometry.vertices.push(new THREE.Vector3(point6.x, point6.y, z));
                    geometry.vertices.push(new THREE.Vector3(point4.x, point4.y, z));
                }
            }
        }
    }

    var line = new THREE.Line(geometry, new THREE.LineBasicMaterial({ color: color, transparent: true, opacity: 0.4 }), THREE.LinePieces);
    group.add(line);

    this.group = group;
};
